package everydaydo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import test.Test3;

public class MusicPlay {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args){
		InputStream ips = Test3.class.getResourceAsStream("2.txt");
	    BufferedReader bd = new BufferedReader( new InputStreamReader(ips));
	    String line;
	    List<String> inf = new ArrayList<String>();
	    try {
			while((line=bd.readLine())!=null){
				inf.add(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				bd.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				ips.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	    inf.remove(0);
	    int group=1;
	    Play p = new Play();;
	    for(String s : inf){
	    	if(!s.startsWith("PRE")&&!s.startsWith("NEXT")&&!s.startsWith("PLAY")){
	    		int songNum = Integer.valueOf(s.split(" ")[0]);
	    		p = new Play(songNum);
	    		System.out.println("*********Group"+group+"**********");
	    		group++;
	    	}
	    	if(s.startsWith("PRE")){
	    		p.preSong();
	    		System.out.println(p.showNowSong());
	    	}
	    	if(s.startsWith("NEXT")){
	    		p.nextSong();
	    		System.out.println(p.showNowSong());
	    	}
	    	if(s.startsWith("PLAY")){
	    		int song =Integer.valueOf(s.split(" ")[1]);
	    		p.playSong(song);
	    		System.out.println(p.showNowSong());
	    	}
	    	
	    }
	    

	}

}
class Play{
	private int nowSong;
	private List<Integer> playList;
	private List<Integer> historyList;
	boolean begin = false;
	public Play(){
		
	}
	public Play(int songNum){
		historyList = new ArrayList<Integer>();
		historyList.add(1);
		playList = new ArrayList<Integer>();
		for(int i =1;i<=songNum;i++){
			playList.add(i);
		}
	}
	
	public void nextSong(){
		begin();
		if(this.nowSong!=playList.size()){
			nowSong=playList.get(nowSong);
			historyList.add(nowSong);
		}
	}
	public void preSong(){
		begin();
		historyList.remove(historyList.size()-1);
		if(historyList.size()!=0){
			nowSong=historyList.get(historyList.size()-1);
		}else{
			nowSong=playList.get(0);
		}
	}
	public void playSong(int index){
		nowSong = playList.get(index-1);
		if(historyList.get(historyList.size()-1)!=nowSong){
		   historyList.add(nowSong);
		}
	}
	public void begin(){
		if(!begin){
			this.begin=true;
			this.nowSong=1;
			historyList.add(1);
			return;
		}
	}
	public int showNowSong(){
		return nowSong;
	}
}
